
package com.baosight.risk.common.quartz;

        import com.baosight.risk.api.fltd.FlaaNodeAuthoService;
        import com.baosight.risk.api.fltd.FltdProcessService;
        import com.baosight.risk.api.fltd.FltdTaskService;
        import com.baosight.risk.api.riim.RiimRiskImportService;
        import com.baosight.risk.api.rire.RireRiskWarningStatusService;
        import com.baosight.risk.api.system.ISysRoleService;
        import com.baosight.risk.api.xtzy.IXtzyCompanyService;
        import com.baosight.risk.api.xtzy.IXtzyRoleUserService;
        import com.baosight.risk.api.xtzy.IXtzyUserService;
        import com.baosight.risk.common.bean.BeanUtils;
        import com.baosight.risk.common.result.AjaxResult;
        import com.baosight.risk.common.utils.DateUtils;
        import com.baosight.risk.service.fltd.entity.FltdTask;
        import com.baosight.risk.service.fltd.vo.FltdTaskVO;
        import com.baosight.risk.service.fltd.vo.NodeTransitionVO;
        import com.baosight.risk.service.riim.entity.RiimRiskImport;
        import com.baosight.risk.service.riim.vo.RiimRiskImportVO;
        import com.baosight.risk.service.rire.vo.RiskWarningStatusVO;
        import com.baosight.risk.service.system.entity.SysRole;
        import com.baosight.risk.service.xtzy.entity.XtzyCompany;
        import com.baosight.risk.service.xtzy.entity.XtzyRoleUser;
        import com.baosight.risk.service.xtzy.entity.XtzyUser;
        import com.baosight.risk.service.xtzy.vo.XtzyCompUserVO;
        import lombok.extern.slf4j.Slf4j;
        import org.apache.commons.collections.ListUtils;
        import org.apache.commons.lang.ObjectUtils;
        import org.apache.commons.lang3.StringUtils;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Component;

        import java.util.*;
        import java.util.stream.Collectors;

/**
 * @author duanyongqiang
 * <p>
 * 定时任务
 * 重点风险披露报告
 */
@Slf4j
@Component("rireRIskReportTaskAutomatic")
public class RireRIskReportTaskAutomatic {
    @Autowired
    private FltdTaskService fltdTaskService;

    @Autowired
    private FltdProcessService fltdProcessService;

    @Autowired
    private RireRiskWarningStatusService rireRiskWarningStatusService;

    @Autowired
    private IXtzyUserService xtzyUserService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IXtzyRoleUserService roleUserService;

    @Autowired
    private FlaaNodeAuthoService flaaNodeAuthoService;

    @Autowired
    private RiimRiskImportService riimRiskImportService;



    public void testParams(String params, String jobId) throws Exception {
        System.out.println("进入定时任务————重点风险披露报告自动提交");
        List<String> flowNos=new ArrayList<String>();
        flowNos.add("fengxianshishifangan");
        flowNos.add("fengxianpilubaogao");
        //查询风险披露报告和风险实施方案待分管领导审批的节点
        List<FltdTask> fltdTaskList=this.queryFltdTaskList(flowNos,"open","B06");
        List<String> flowNoList=new ArrayList<String>();
        flowNoList.add("fengxianziping");
       /* //查询风险自评待分管领导审批的节点
        List<FltdTask> fltdTaskListData=this.queryFltdTaskList(flowNoList,"open","A02");
        fltdTaskList.addAll(fltdTaskListData);*/
        for (FltdTask task:fltdTaskList) {
            XtzyUser xtzyUser=new XtzyUser();
            if("fengxianshishifangan".equals(task.getFlowNo())){
                RiimRiskImport riimRiskImport=riimRiskImportService.getImport(task.getBizGuid());
                if(riimRiskImport!=null){
                    xtzyUser=xtzyUserService.getUserByStatus(riimRiskImport.getDutyUserCode());
                }
            }else if("fengxianpilubaogao".equals(task.getFlowNo())){
                RiskWarningStatusVO statusVO=rireRiskWarningStatusService.getWarningById(task.getBizGuid());
                if(statusVO!=null && StringUtils.isNotEmpty(statusVO.getDeptUserCode())){
                    xtzyUser=xtzyUserService.getUserByStatus(statusVO.getDeptUserCode());
                }
            }


            Date factStartDate=task.getFactStartDate();
            Date addDateStart=DateUtils.plusDays(factStartDate,2);
            if(addDateStart.compareTo(new Date())==-1){
                FltdTaskVO taskVoParam=new FltdTaskVO();
                BeanUtils.copyBeanProp(taskVoParam,task );
                Map<String, Object> resultMap = flaaNodeAuthoService.getTransitionButton(taskVoParam);
                List<NodeTransitionVO> transitions = (List<NodeTransitionVO>) resultMap.get("transitions");
                if(transitions.size()>0){
                    List<NodeTransitionVO>  transitionsList=transitions.stream().filter(nodeTransitionVO -> nodeTransitionVO.getTransitionKey().contains("submit")).collect(Collectors.toList());
                    taskVoParam.setSubmitButton(transitionsList.get(0));
                }
                List<XtzyUser> userListData=new ArrayList<XtzyUser>();
                userListData.add(xtzyUser);
                taskVoParam.setCustomUsers(userListData);
                taskVoParam.setVariables(this.joinParam(xtzyUser));
                //获取当前流程扭转人

                XtzyUser currentXtzyUser=xtzyUserService.getUserByStatus(taskVoParam.getCurrUserCode());
                fltdProcessService.doSubmit(currentXtzyUser,taskVoParam);
            }
        }
        //查询风险披露报告和风险实施方案领导审批节点
        List<FltdTask> list=this.queryFltdTaskList(flowNos,"open","B02");
/*        //查询风险自评的领导审批的节点
        List<FltdTask> riskEvaluationList=this.queryFltdTaskList(flowNoList,"open","A03");
        list.addAll(riskEvaluationList);*/
        for (FltdTask task: list) {
            XtzyUser xtzyUser=new XtzyUser();
            List<XtzyUser> userList=new ArrayList<XtzyUser>();
            Date startDate=task.getFactStartDate();
            Date addDateStart=DateUtils.plusDays(startDate,2);
            if(addDateStart.compareTo(new Date())==-1){
                SysRole role=new SysRole();
                role.setRoleKey("RISK03");
                List<SysRole> sysRoleList=sysRoleService.selectRole(role);
                if(sysRoleList.size()>0){
                    List<XtzyRoleUser> roleUserList=roleUserService.selectRoleUserList("BGTAZS00",sysRoleList.get(0).getId());
                    if(roleUserList.size()>0){
                        xtzyUser=xtzyUserService.getUserByStatus(roleUserList.get(0).getUserCode());
                        userList.add(xtzyUser);
                    }
                }
                FltdTaskVO taskVoParam=new FltdTaskVO();
                BeanUtils.copyBeanProp(taskVoParam,task);
                Map<String, Object> resultMap = flaaNodeAuthoService.getTransitionButton(taskVoParam);
                List<NodeTransitionVO> transitions = (List<NodeTransitionVO>) resultMap.get("transitions");
                if(transitions.size()>0){
                    List<NodeTransitionVO>  transitionsList=transitions.stream().filter(nodeTransitionVO -> nodeTransitionVO.getTransitionKey().contains("submit")).collect(Collectors.toList());
                    taskVoParam.setSubmitButton(transitionsList.get(0));
                }
                taskVoParam.setCustomUsers(userList);
                taskVoParam.setVariables(this.joinParam(xtzyUser));
                //获取当前流程扭转人
                XtzyUser currentXtzyUser=xtzyUserService.getUserByStatus(taskVoParam.getCurrUserCode());

                fltdProcessService.doSubmit(currentXtzyUser,taskVoParam);
            }
        }

        System.out.println("执行结束————重点风险披露报告自动提交");
    }
    private List<FltdTask> queryFltdTaskList(List<String> flowNos,String status,String nodeCode){
        FltdTaskVO taskVo=new FltdTaskVO();
        taskVo.setFlowNoList(flowNos);
        taskVo.setStatus(status);
        taskVo.setNodeCode(nodeCode);
        return fltdTaskService.selectTaskListInFlowNo(taskVo);
    }
    private Map<String, Object> joinParam(XtzyUser xtzyUser){
        Map<String, Object> param=new HashMap<String, Object>();
        param.put("quartzParam","1");
        param.put("currentExaminerCode",xtzyUser.getUserCode());
        param.put("currentExaminerName",xtzyUser.getUserDesc());
        return param;
    }
    public void testNoParams() {
        log.error("执行无参方法");
    }
}
